/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import core.DriverManager;
import core.MessageManager;
import core.ScreenEngine;
import datamodel.Message;
import java.util.Date;
import network.Connection;
import network.PacketBuilder;
import utils.Utils;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class SendMessageForm extends Form
{
    private static final int SEND_COMMAND_ID = 1;
    
    //private TextField messageText;
    private TextArea messageText;
    
    public SendMessageForm()
    {
        super("message");
        
        init();
    }
    
    private void init() 
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        
        //Компоненты
        addComponent(new Label("message_text"));
//        messageText = new TextField();
//        messageText.setSelectCommandText("");
//        messageText.setConstraint(TextArea.ANY);    
//        messageText.setInputModeOrder(new String[]{"АБВ", "абв", "123", "ABC","abc"});
//        messageText.setGrowByContent(true);      
        messageText = new TextArea(5, 30);
        messageText.setSelectCommandText("");
        messageText.setConstraint(TextArea.ANY);
        messageText.setEditable(true);
        messageText.setGrowByContent(true);
        messageText.getSelectedStyle().setBorder(Border.createLineBorder(2));
        messageText.getUnselectedStyle().setBorder(Border.createLineBorder(2));
        
        addComponent(messageText);
                
        //Команды
        addCommand(new BackCommand());
        
        Command send = new Command("send", SEND_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                //Отправляем сообщение
                sendMessage();

                evt.consume();
            }
        };
        addCommand(send); 
    }
    
    public void clean()
    {
        //Убираем старый текст
        messageText.setText("");
    }
    
    private void sendMessage()
    {
        String text = messageText.getText();
        
        //Если не ввели текст
        if (text.equals(""))
        {
            MessageBox.showModeless("error","no_message_text");
            return;        
        }
        
        //Отображаем форму прогресса
        ScreenEngine.getEngine().push(new ProgressForm());
        
        //отправляем запрос
        byte[] data = PacketBuilder.createSendMessageRequest(DriverManager.getManager().getDriverID(), DriverManager.MESSAGE_TO_DISPETCHER, text);
        boolean result = Connection.getConnection().send(data);
        
        //Добавляем сообщение в менеджер(если отправили)
        if (result)
        {
            Message sendedMessage = new Message(Utils.timeToString(new Date()) + " я написал:", text);
            MessageManager.getManager().addMessage(sendedMessage);
        }
        
        //Убераем форму загрузки
        ScreenEngine.getEngine().pop(true);

        //Говорим отправили или нет сообщение
        if (result)
        {
            MessageBox.showModeless("", "message_sent");
        }
        else
        {
            MessageBox.showModeless("error", "message_not_sent");
        }
    }
}
